package co.fitstart.market.web.controller.course.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.MarketCourseRoundService;
import co.fitstart.market.web.interceptor.MobileDetect;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;
import co.fitstart.market.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/courses")
public class CourseRestController {
    
    @Autowired
    private MarketCourseRoundService marketCourseRoundService;
    
    private static final Logger logger = LoggerFactory.getLogger(CourseRestController.class);
    
    /**
     * 
     * 已加入群
     * @param user 
     * @return
     */
    @RequestMapping(value = "/join-group", method = RequestMethod.POST)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String join(@Secure User user) {
        
        Result result = new Result();
        
        try {
            
            MarketCourseRound courseRound = marketCourseRoundService.load(user);
            marketCourseRoundService.joinGroup(courseRound);
            
            result.success().message("加入群成功");
            
        } catch (Exception e) {
            logger.error("join - ", e);
            result.fail("500").message("加入群失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
}
